<template>
	<div>
		<el-dialog :visible.sync="visible1" title="产生源信息" top="3%" width="30%">
			<el-form ref="form" :model="wasteInfo" :rules="rules" label-width="100px">
				<el-form-item label="产生源编码" prop="sourceCode">
					<el-input v-model="wasteInfo.sourceCode" style="width: 100%;"></el-input>
				</el-form-item>
				<el-form-item label="标识编号" prop="identityCode">
					<el-input v-model="wasteInfo.identityCode" style="width: 100%;"></el-input>
				</el-form-item>
				<el-form-item label="产生源名称" prop="sourceName">
					<el-input v-model="wasteInfo.sourceName" style="width: 100%;"></el-input>
				</el-form-item>
				<el-form-item label="产生源类型" prop="sourceType">
					<el-select v-model="wasteInfo.sourceType" style="width: 100%;">
						<el-option :label="item.label" :value="item.value" v-for="(item,index) in typeList"></el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="危废" prop="relationList">
					<el-select @focus="getWasteList()" style="width: 100%;" multiple v-model="wasteInfo.relationList"
						placeholder="请配置危废" @change="changeWaste">
						<el-option :label="item.wasteCode + ' ' +item.wasteName" :value="item.id"
							v-for="(item,index) in wasteList"></el-option>
					</el-select>
				</el-form-item>

				<!-- <el-form-item label="生产设施" prop="equipmentId">
					<template scope="scope">
						<el-select style="width: 100%;" v-model="wasteInfo.equipmentId" placeholder="请配置生产设施"
							filterable>
							<el-option :label="item.equipmentName + ' ' +item.equipmentNo" :value="item.id"
								v-for="(item,index) in equipmentBaseList"></el-option>
						</el-select>
					</template>
				</el-form-item> -->

				<el-form-item label="产生源标识" prop="officialSourceId" v-if="wasteInfo.sourceType != 'SECONDARY_SOURCE'">
					<el-input v-model="wasteInfo.officialSourceId" placeholder="产生源标识用于省系统API对接，请勿随意填写"
						style="width: 100%;"></el-input>
				</el-form-item>
				<el-form-item label="产生源校验码" prop="officialToken" v-if="wasteInfo.sourceType != 'SECONDARY_SOURCE'">
					<el-input v-model="wasteInfo.officialToken" placeholder="产生源校验码用于省系统API对接，请勿随意填写"
						style="width: 100%;">
					</el-input>
				</el-form-item>

				<!-- <el-form-item label="关联类型">
					<el-radio-group v-model="wasteInfo.outletType" @input="changeOutLetType">
						<el-radio label="废气排口"></el-radio>
						<el-radio label="废水排口"></el-radio>
					</el-radio-group>
				</el-form-item>

				<el-form-item v-if="wasteInfo.outletType =='废水排口'" label="废水排口" prop="waterOutletId">
					<el-select style="width: 100%;" v-model="wasteInfo.waterOutletId" placeholder="选择废水排口">
						<el-option :label="item.waterOutletName + ' ' +item.waterOutletNo" :value="item.id"
							v-for="(item,index) in outletList"></el-option>
					</el-select>
				</el-form-item>

				<el-form-item v-if="wasteInfo.outletType =='废气排口'" label="废气排口" prop="gasOutletId">
					<el-select style="width: 100%;" v-model="wasteInfo.gasOutletId" placeholder="选择废气排口">
						<el-option :label="item.gasOutletName + ' ' +item.gasOutletNo" :value="item.id"
							v-for="(item,index) in outletList"></el-option>
					</el-select>
				</el-form-item> -->


			</el-form>

			<div style="text-align: center;" slot="footer">
				<el-button v-if="wasteInfo && wasteInfo.id" type="danger" @click="deleteThis">删除</el-button>
				<el-button type="primary" @click="saveOrUpdate">保存</el-button>
				<el-button @click="cancle">取消</el-button>
			</div>
		</el-dialog>
	</div>
</template>

<script>
	let that
	export default {
		props: ['entId', 'editData'],
		name: 'addWasteSource',
		components: {},
		data() {
			return {
				params: {},
				visible: false,
				visible1: false,
				wasteInfo: {},
				unitList: [],
				typeList: [{
						label: '非生产性产生源',
						value: 'NOT_PRODUCT_SOURCE'
					},
					{
						label: '生产性产生源',
						value: 'PRODUCT_SOURCE'
					},
					{
						label: '次生危废产生源',
						value: 'SECONDARY_SOURCE'
					},
					{
						label: '其他',
						value: 'OTHER'
					}
				],
				enterpriseId: '',
				rules: {
					sourceCode: [{
						required: true,
						message: '请输入产生源编码',
						trigger: 'blur'
					}],
					sourceName: [{
						required: true,
						message: '请输入产生源名称',
						trigger: 'blur'
					}],
					sourceType: [{
						required: true,
						message: '请选择产生源类型',
						trigger: 'blur'
					}],
					entWasteId: [{
						required: true,
						message: '请选择关联的危废',
						trigger: 'blur'
					}],
				},
				wasteList: [],
				outletList: [],
				currentSource: {},
				equipmentBaseList: []
			}
		},
		created: function() {},
		mounted: function() {
			that = this
			that.params.ticketId = that.$store.state.token
			that.getWasteList()

			// that.ajax({
			// 	url: '//productionEquipment/getOriginProductionEquipmentBaseList',
			// 	data: {},
			// 	success: function(data) {
			// 		that.equipmentBaseList = data.data;
			// 	}
			// });
		},
		methods: {

			open(editData) {
				if (editData) {
					that.wasteInfo = editData
					that.getWasteList()
				} else {
					that.wasteInfo = {}
				}
				that.visible1 = true
			},

			cancle() {
				//console.log("sss")
				that.visible1 = false
				that.$emit("cancle", true)
			},

			changeWaste(value) {
				that.wasteInfo.entWasteId = value
				that.$forceUpdate()
			},

			changeOutLetType(value) {
				let thisApi
				if (value == "废气排口") thisApi = "gasOutlet/getOriginalGasOutletBaseList"
				if (value == "废水排口") thisApi = "waterOutlet/getOriginalWaterOutletBaseList"

				that.ajax({
					url: thisApi + '?ticketId=' + that.params.ticketId,
					data: JSON.stringify({}),
					contentType: 'application/json',
					success: function(result) {
						if (result.status && result.data) {
							that.outletList = result.data
							that.$forceUpdate()
						}
					}
				})
			},

			//删除危废
			deleteThis() {
				this.$confirm('此操作将删除此数据, 是否继续?', '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning'
				}).then(() => {
					that.ajax({
						url: '/hw/wasteSource/deleteWasteSourceById?ticketId=' + that.params.ticketId,
						data: JSON.stringify([that.wasteInfo.id]),
						contentType: 'application/json',
						success: function(result) {
							if (result.status && result.data) {
								that.visible1 = false
								that.$message.success("成功！")
								that.$emit("success", true)
							}
						}
					})
				})
			},


			getWasteList: function() {
				var _this = this;
				var param = {
					entId: _this.entId
				}
				that.ajax({
					url: '/hw/entWaste/getEntWasteInfoByEntId.htm?ticketId=' + that.params.ticketId,
					dataType: 'json',
					data: param,
					success: function(result) {
						if (result.status == 1) {
							_this.wasteList = result.data
						}
					}
				})
			},
			saveOrUpdate: function() {
				var _this = this;
				_this.$refs['form'].validate(function(valid) {
					if (valid) {
						var param = {
							id: _this.wasteInfo.id,
							entId: _this.entId,
							sourceCode: _this.wasteInfo.sourceCode,
							identityCode: _this.wasteInfo.identityCode,
							sourceName: _this.wasteInfo.sourceName,
							sourceType: _this.wasteInfo.sourceType,
							officialSourceId: _this.wasteInfo.officialSourceId,
							officialToken: _this.wasteInfo.officialToken,
							officialId: _this.wasteInfo.officialId,
							relationList: _this.wasteInfo.relationList,
							waterOutletId: _this.wasteInfo.waterOutletId,
							gasOutletId: _this.wasteInfo.gasOutletId,
							equipmentId: _this.wasteInfo.equipmentId,
						}
						that.ajax({
							url: '/hw/wasteSource/saveOrUpdateWasteSource?ticketId=' + that.params
								.ticketId,
							data: JSON.stringify(param),
							contentType: 'application/json',
							success: function(data) {
								that.data = that.$options.data()
								that.visible1 = false
								if (data.status == 1) {
									that.$message.success("保存成功！")
									if (!param.id) {
										that.$emit("success", true)
									} else {
										that.$emit("success", true)
									}
								} else {}
							}
						});
					}
				})
			},
			getWasteTable: function(val) {
				this.visible = true;
			},
			handleSelectionChange: function(val) {
				this.multipleSelection = val;
			},
			getUnitList: function() {
				var _this = this;
				that.ajax({
					url: '/codeType/listCodeValue.htm?ticketId=' + that.params.ticketId,
					data: {
						typeCode: 'UNIT_TYPE'
					},
					success: function(data) {
						if (data.status == 1) {
							_this.unitList = data.data;
						}
					}
				});
			},

		}
	}
</script>

<style>

</style>